package com.spring.dynamic.jar.controller;

import com.spring.dynamic.jar.core.AddonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ContextLoader;

import java.io.File;
import java.net.MalformedURLException;

/**
 * DemoDynamicController
 *
 * @author Brant Liu <br> 邮箱：<br>lbf1988@qq.com <br>日期：<br>2018/04/09
 * @version 1.0.0
 */
@Controller
public class DemoDynamicController {
    public static final String PATH_ADDONS      = "/WEB-INF/addons";
    public static final String PATH_ADDONS_ROOT = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath(PATH_ADDONS);

    @RequestMapping(value="/")
    public String sayHello(ModelMap model){
        model.put("title","你好,,,,!!! <br/> addons-demo.jar:<a href=\"/addon/demo\" target=\"_blank\">test addon demo</a>......<a href=\"/addon1\" target=\"_blank\">dynamic add addon1</a> <br/> jarsrun-demo.jar:<a href=\"/test\" target=\"_blank\">test jarsrun demo</a>......<a href=\"/addon2\" target=\"_blank\">dynamic add addon2</a>");
        return "index";
    }

    @RequestMapping(value="/addon1")
    public String addon1(ModelMap model) throws MalformedURLException {
        File file = new File(PATH_ADDONS_ROOT + "/addons-demo.jar");
        AddonUtil.assembleAddon(file,"com.addondemo");
        model.put("title","addons-demo.jar:<a href=\"/addon/demo\">test addon demo</a>");
        return "index";
    }

    @RequestMapping(value="/addon2")
    public String addon2(ModelMap model) throws MalformedURLException {
        File file = new File(PATH_ADDONS_ROOT + "/jarsrun-demo.jar");
        AddonUtil.assembleAddon(file,"com.jarsrun.test.controller");
        model.put("title","jarsrun-demo.jar:<a href=\"/test\">test jarsrun demo</a>");
        return "index";
    }
}
